/***************************************************************************
 *            echo_server.c
 *
 *  Fri May 25 21:26:31 2007
 *  Copyright  2007  kf701
 *  Email <kf701.ye AT gmail.com>
 ****************************************************************************/

#include <pthread.h>

#include "factory.h"

void *thread_main( void *data )
{
	/* detach thread */
	pthread_detach( pthread_self() );

	int fd = *((int*)data);
	char buf[1024];

	int ret = read( fd, buf, sizeof(buf) );
	if( ret <= 0 )
	{
		sys_err("%s: read err\n", __func__);
		goto err_p;
	}
	sys_debug("read = %s", buf);

	/* echo data to client */
	write( fd, buf, ret );
err_p:
	close( fd );
	free( data );
	return (void*)0; /* thread exit */
}

void run(int fd)
{
	int *data = malloc( sizeof(int) );
	*data = fd;
	create_normal_thread( thread_main, data );
}

int main()
{
	debug_verbose = 4;
	sys_debug("tcp listen port 8080 ...\n");
	tcp_server_select_2( 8080, run );
	return 0;
}
